<template>
	<view class="content">
		<view class="logonBox">
			<image class="logon" src="../../static/image/app_icon.png" />
			<view class="logonText">趣味答题</view>
		</view>
		<view class="logonIn">
			<view class="userInfo">
				<view class="title">手机号</view>: 
				<input class="textInput" type="text" v-model="phone" placeholder="输入手机号">
			</view>
			<view class="userInfo">
				<view class="title">密码</view>: 
				<input class="textInput" type="password" v-model="password" placeholder="输入密码">
			</view>
			<view class="userInfo" v-if="!isLogon">
				<view class="title">确认密码</view>: 
				<input class="textInput" type="password" v-model="onPassword" placeholder="输入密码">
			</view>
			<view class="userInfo" v-if="!isLogon">
				<view class="title">验证码：</view>
				<view class="testCode">
					<input class="code" v-model="code" type="text">
					<view class="timing">60s后重新发送</view>
				</view>
			</view>
		</view>
		
		<view class="bottonBut" @click="goLogon" v-if="!isLogon">注册</view>
		<view class="haveAdmin" v-if="!isLogon" @click="isLogon = true">已有账号，登录</view>
		<view class="bottonBut" @click="userLogin" v-if="isLogon">登录</view>
		<view class="haveAdmin" v-if="isLogon" @click="isLogon = false">注册新账号</view>
	</view>
</template>

<script>
	export default {
		data(){
			return {
				code: '', // 验证码
				phone: '', //用户手机
				onPassword: '', // 第一次输入的密码
 				password: '', // 密码
				isLogon: false // 是否完成注册
			}
		},
		methods: {
			// 登录
			userLogin() {
				let tips = {
					phone: '账号不能为空',
					password: '密码不能为空'
				}
				for (let i in tips) {
					if (!this[i]) {
						uni.showToast({
							title: tips[i],
							icon: 'none'
						})
						return
					}
				}
				uni.clearStorageSync('userInfo')
				this.$http.userLogin({phone: this.phone, password: this.password}).then((res) => {
					uni.showToast({
						title: '登录成功',
						icon: 'none'
					})
					console.log(res)
					uni.setStorageSync('userInfo', res)
					uni.setStorageSync('token', res.token)
					this.$store.commit('setUserInfo', res)
					uni.switchTab({
						url: '/pages/index/index'
					})
				}).catch((err) => {
					uni.showToast({
						title: err,
						icon: 'none'
					})
				})
			},
			// 注册
			goLogon() {
				let tips = {
					phone: '账号不能为空',
					password: '密码不能为空',
					onPassword: '密码不能为空',
					code: '验证码不能为空'
				}
				for (let i in tips) {
					if (!this[i]) {
						uni.showToast({
							title: tips[i],
							icon: 'none'
						})
						return
					}
				}
				// 验证密码是否相同
				if (this.onPassword !== this.password) {
					uni.showToast({
						title: '两次密码需保持一致',
						icon: 'none'
					})
					return
				}
				
				//  注册
				let param = {
					phone: this.phone,
					password: this.password,
					code: this.code
				}
				this.$http.logonIn(param).then((res) => {
					uni.showToast({
						title: '注册成功',
						icon: 'none'
					})
					this.isLogon = true
				}).catch((err) => {
					uni.showToast({
						title: err,
						icon: 'none'
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	.content{
		position: fixed;
		top: 0;
		left: 0;
		bottom: 0;
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		background-image: linear-gradient(rgba(4,246,223, 0.6), rgba(206,249,81,0.6));
	}
	.logonBox{
		display: flex;
		align-items: center;
		margin-top: 300rpx;
		.logon{
			width: 100rpx;
			height: 100rpx;
		}
		.logonText{
			margin-left: 20rpx;
			font-size: 36rpx;
			// color: rgba(4,246,223,1);
			color: #111;
			font-weight: 700;
		}
	}
	
	.logonIn{
		width: 600rpx;
		margin-top: 60rpx;
		padding: 10rpx 30rpx 30rpx;
		box-shadow: 0rpx 0rpx 20rpx #ddd;
		background-color: #fff;
		border-radius: 30rpx;
		.userInfo{
			display: flex;
			align-items: center;
			padding: 30rpx 0;
			border-bottom: 1px solid #eee;
			.title{
				width: 130rpx;
				text-align-last: justify;
				font-size: 32rpx;
				font-weight: 500;
				&:after {
						content: '';
						width: 100%;
						height: 0;
						// display: inline-block;
						visibility: hidden;
					}
			}
			.textInput{
				margin-left: 20rpx;
				font-size: 28rpx;
			}
		}
		.testCode{
			display: flex;
			align-items: center;
			font-size: 20rpx;
			.code{
				width: 200rpx;
				font-size: 28rpx;
			}
			.timing{
				padding: 10rpx 20rpx;
				margin-left: 40rpx;
				border-radius: 10rpx;
				background-color: rgba(4,246,223, 0.6);
			}
		}
	}
  
	.bottonBut{
		width: 360rpx;
		height: 80rpx;
		line-height: 80rpx;
		margin-top: 60rpx;
		border-radius: 30rpx;
		text-align: center;
		background: #fff;
		color: #000;
	}
	.haveAdmin{
		margin-top: 20rpx;
		font-size: 24rpx;
		color: #000;
		border-bottom: 1px solid rgba(4,246,223, 1);
	}
</style>
